package com.zhx.threadcoreknowledge.synchromized;

/**
 * @author ：zhanghaixuan
 * @date ：Created in 2019-10-02 10:17
 * 用途         ： 对象锁实例3,方法锁形式
 */
public class SynchronizedObjectMethod3 implements Runnable{

    private static SynchronizedObjectMethod3 instance = new SynchronizedObjectMethod3();

    @Override
    public void run() {
        try {
            method();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Thread thread1 = new Thread(instance);
        Thread thread2 = new Thread(instance);
        thread1.start();
        thread2.start();

        while (thread1.isAlive() || thread2.isAlive()){

        }

        System.out.println("finashed");
    }

    public synchronized void method() throws InterruptedException {
        System.out.println("我是对象锁的方法修饰符形式,我叫:"+Thread.currentThread().getName());
        Thread.sleep(3000);
        System.out.println(Thread.currentThread().getName()+"运行结束了");
    }
}
